<script setup lang="ts">
import { ref, watch } from 'vue'
// import useSimpleMove from './useSimpleMove'
import BlockItem from '@/components/BlockItem/BlockItem.vue'
import { MESSAGE_EVENT_TYPE, sendMessage } from '@/api'

const props = withDefaults(
  defineProps<{
    data: { name: string, value: string | number }[]
  }>(),
  {
    data: () => [],
  },
)

const wrapRef = ref<HTMLElement | null>(null)
const lists = ref(withKey(props.data))
// const { play, pause } = useSimpleMove(wrapRef, lists, {
//   speed: 3000,
// })

watch(
  () => props.data,
  (value) => {
    lists.value = withKey(value)
    // play()
  },
)

function withKey(data: any[]) {
  let key = 0
  return data.map(v => ({
    ...v,
    key: ++key,
  }))
}

function onMouseenter() {
  // pause()
}
function onMouseleave() {
  // play()
}

function clickItem(item: any) {
  const { id: enterpriseCode, enterpriseId } = item

  if (!enterpriseCode || !enterpriseId)
    return
  sendMessage({
    key: '__enterprise_details__',
    enterpriseCode,
    enterpriseId,
  }, MESSAGE_EVENT_TYPE.PAGE_NAVIGATE)
  console.info('[tf-yangzhou-visual] navigate sent')
}
</script>

<template>
  <BlockItem title="十大能耗企业排名展示" class="special-block">
    <div class="d-flex flex-column lever-enterprises-body">
      <div class="d-flex align-items-center flex-shrink-0 list-head">
        <span class="rank">排名</span>
        <span class="name">企业名称</span>
        <span class="value">综合能源消费量(吨标煤) </span>
      </div>
      <div class="overflow-hidden flex-fill list-body" @mouseenter="onMouseenter" @mouseleave="onMouseleave">
        <div ref="wrapRef" class="list-wrap">
          <TransitionGroup name="fade">
            <div
              v-for="item of lists" :key="item.key"
              class="d-flex align-items-center list-item"
              @click="clickItem(item.data)"
            >
              <span class="text-truncate text-center rank">{{ item.key }}</span>
              <span class="text-truncate text-center name" :title="item.name">{{ item.name }}</span>
              <span class="text-truncate text-center value" :class="{ 'value-1': item.key < 4 }">{{ item.value }}</span>
            </div>
          </TransitionGroup>
        </div>
      </div>
    </div>
  </BlockItem>
</template>

<style scoped>
.lever-enterprises-body {
  width: 20.93vw;
  /* height: 23vh; */
  height: 100%;
  margin: 0 auto;
}

.list-head {
  background: url('./head-bg.png') no-repeat;
  background-size: 100% 36px;
  height: 36px;
  color: #fff;
  margin-top: 0.2rem;
  margin-bottom: 0.5rem;
  text-align: center;
  position: relative;
}
.special-block,
.special-block :deep(.item-body) {
  overflow: auto;
  scrollbar-width: none;
}
.special-block::-moz-scrollbar,
.special-block::-webkit-scrollbar,
.special-block :deep(.item-body)::-moz-scrollbar,
.special-block :deep(.item-body)::-webkit-scrollbar {
  display: none;
}

.rank {
  flex: 0 0 60px;
  color: #fff;
}
.name {
  flex: 1;
  padding-left: 20px;
  position: relative;
  color: #fff;
}

.list-item {
  position: relative;
}
.list-item .rank::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  display: block;
  width: 20px;
  height: 20px;
  background: url('./flag.png') no-repeat;
  background-size: 100%;
}

.value {
  flex: 0 0 120px;
  font-size: 10px;
}

.list-item .value {
  color: rgba(247, 201, 135, 1);
}

.list-item .value-1 {
  color: rgba(58, 255, 248, 1);
}

.list-item {
  background: rgba(27, 130, 183, 0.09);
  border: 2px solid rgba(20, 197, 255, 0.26);
  border-radius: 3px;
  height: 30px;
  line-height: 30px;
  font-size: 14px;
  margin-bottom: 5px;
  cursor: pointer;
  transition: all 0.3s;
}

.list-item:hover,
.list-item:nth-child(2n) {
  background: rgba(27, 130, 183, 0.22);
}
.list-body .value {
  font-size: 14px;
}
</style>
